package com.android.antelopeapp.common;

import android.util.Log;

import java.util.HashMap;
import java.util.Map;

/**
 * @author phk52
 * 该类用于实现对列表元素的选择
 */
public abstract class SelectableViewList {
	public static final int NONE = -1;
	private Map<Integer,Selectable> selectAbleList = new HashMap<>();
	private int curSelectedPos = -1;
	
	private void changeSelectedState(int pos,boolean state){
		if(pos>=0 && selectAbleList.get(pos)!=null){
            Log.v("SelectableViewList","changeSelectedState pos:"+pos+" state:"+state);
            selectAbleList.get(pos).setSelected(state);
		}
	}
	
	/**
	 * 获取当前选定的位置
	 * */
	public int getCurSelectedPos(){
		return curSelectedPos;
	}
	
	/**取消选择某一元素*/
	public void unSelected(int pos){
		changeSelectedState(pos, false);
	}
	/**改变选择状态时触发*/
	abstract public void onSelected(int pos);
	
	/**
	 * 多次选择某一元素
	 * */
	public void selectSameEle(int pos){
		//todo
	}
	
	/**选择某一元素*/
	public synchronized void select(int pos){
		if(curSelectedPos == pos){
			selectSameEle(pos);
		}else{
			unSelected(curSelectedPos);
			changeSelectedState(pos, true);
			onSelected(pos);
            curSelectedPos = pos;
		}
	}
	
	/**
	 * 添加元素
	 * */
	public void putItem(int pos,Selectable sa){
		this.selectAbleList.put(pos,sa);
	}
	/**
	 * 获取元素
	 * */
	public Selectable getItem(int pos){
		return this.selectAbleList.get(pos);
	}
	
	public static interface Selectable{
		void setSelected(boolean state);
	}
}
